import { Service } from "../service";
import { IChildItem } from "../model";
export function childItemReducer(childItem: IChildItem) {
  return childItem;
}
export const resolvers = {
  Query: {
    childItemById: (_: any, { id }: { id: string }) =>
      Service.getChildItemByIds([id])[0],
    childItems: () => Service.listChildItems(),
  },
  Mutation: {
    addChildItem: (_: any, params: { parent: string; title?: string }) => {
      try {
        const childItem = Service.addChildItem(params);
        Service.save();
        return {
          success: true,
          childItem,
        };
      } catch (err) {
        return { success: false, message: err.message };
      }
    },
    removeChildItemById: (_: any, { id }: { id: string }) => {
      const childItem = Service.removeChildItem(id);
      Service.save();
      return {
        success: true,
        childItem,
      };
    },
    removeChildItemByIds: (_: any, { ids }: { ids: string[] }) => {
      const childItems = ids.map((id) => Service.removeChildItem(id));
      Service.save();
      return {
        success: true,
        childItems,
      };
    },
  },
};
